<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增商品信息管理')" />
    <th:block th:include="include :: bootstrap-fileinput-css"/>
    <th:block th:include="include :: summernote-css" />

</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-product-add">
            <div class="form-group">    
                <label class="col-sm-3 control-label">商品名称：</label>
                <div class="col-sm-8">
                    <input name="name" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">商品描述：</label>
                <div class="col-sm-8">
                    <input name="des" class="form-control" type="text">
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-3 control-label">商品简介：</label>
                <div class="col-sm-8">
                    <input type="hidden" class="form-control" name="content">
                    <div class="summernote" id="content"></div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">商品图片：</label>
                <div class="col-sm-8">
                    <input type="hidden" name="imglist">
                    <div class="file-loading">
                        <input class="form-control file-upload" id="imglist" name="file" type="file">
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">销量：</label>
                <div class="col-sm-8">
                    <input name="count" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">库存：</label>
                <div class="col-sm-8">
                    <input name="inventory" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">商品状态：</label>
                <div class="col-sm-8">
                    <select name="status" class="form-control m-b" th:with="type=${@dict.getType('product_status')}">
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">商品地区：</label>
                <div class="col-sm-8">
                    <select id="province" class="form-control m-b" name="provinceName">
                        <option value="-1">--请选择省份--</option>
                    </select>
                    <select  id="city" class="form-control m-b" name="cityName">
                        <option value="-1">--请选择市区--</option>
                    </select>
                </div>




<!--                <div class="col-sm-8">-->
<!--                    <div class="radio-box" th:each="dict : ${@dict.getType('platform_prov')}">-->
<!--                        <input type="radio" th:id="${'prov_' + dict.dictCode}" name="prov" th:value="${dict.dictValue}" th:checked="${dict.default}">-->
<!--                        <label th:for="${'prov_' + dict.dictCode}" th:text="${dict.dictLabel}"></label>-->
<!--                    </div>-->
<!--                    -->

<!--                </div>-->
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">商品价格：</label>
                <div class="col-sm-8">
                    <input name="price" class="form-control" type="text">
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: bootstrap-fileinput-js"/>
    <th:block th:include="include :: summernote-js" />

    <script th:inline="javascript">
        var prefix = ctx + "platform/product"
        $("#form-product-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/add", $('#form-product-add').serialize());
            }
        }

        var List = new Array();
        $(".file-upload").fileinput({
            uploadUrl: ctx + 'common/upload',
            maxFileCount: 5,
            // autoReplace: true
        }).on('fileuploaded', function (event, data, previewId, index) {
            List.push({ FileName: data.response.url, KeyID: previewId })
            const fileNamesArray = List.map(item => item.FileName);

            const resultString = fileNamesArray.join(',');
            $("input[name='" + event.currentTarget.id + "']").val(resultString)
            var url1 = $("input[name='" + event.currentTarget.id + "']").val()
            console.log(url1)        }).on('filesuccessremove', function (event, id, index) {
            for (var i = 0; i < List.length; i++) {
                if (List[i].KeyID == data) {
                    List.splice(i,1)
                }
            }
            const fileNamesArray = List.map(item => item.FileName);

            const resultString = fileNamesArray.join(',');
            $("input[name='" + event.currentTarget.id + "']").val(resultString)
            var url1 = $("input[name='" + event.currentTarget.id + "']").val()
            console.log(url1)        })
    </script>



<script>

    // 前端JS代码
    $(document).ready(function() {
        // 页面加载完成时渲染省份选项
        renderProvinceOptions();

        // 当省份选择框的值发生改变时
        $('#province').change(function() {
            var provinceId = $(this).val(); // 获取选中的省份ID
            if (provinceId != 0) {
                // 发送Ajax请求获取对应省份的市区列表
                $.ajax({
                    url: '/city/getCityListByid',
                    type: 'GET',
                    data: { id: provinceId },
                    success: function(data) {
                        console.log(data)
                        // 清空市区选择框的选项
                        $('#city').empty();
                        // 添加新的市区选项
                        $('#city').append('<option value="-1">--请选择市区--</option>');
                        // 将返回的市区数据添加到市区选择框中
                        $.each(data.data, function(index, city) {
                            $('#city').append('<option value="' + city.districtId + '">' + city.districtName + '</option>');
                        });
                    },
                    error: function(xhr, status, error) {
                        console.error('获取市区列表失败: ' + error);
                    }
                });
            } else {
                // 如果省份选择框选择的是“--请选择省份--”，则清空市区选择框
                $('#city').empty();
                $('#city').append('<option value="0">--请选择市区--</option>');
            }
        });
    });

    // 渲染省份选项的函数
    function renderProvinceOptions() {
        // 发送Ajax请求获取省份列表
        $.ajax({
            url: '/city/getProvinceList',
            type: 'GET',
            success: function(data) {
                // 清空省份选择框的选项
                $('#province').empty();
                // 添加新的省份选项
                console.log(data)
                $('#province').append('<option value="-1">--请选择省份--</option>');
                // 将返回的省份数据添加到省份选择框中
                $.each(data.data, function(index, province) {
                    $('#province').append('<option value="' + province.districtId + '">' + province.districtName + '</option>');
                });
            },
            error: function(xhr, status, error) {
                console.error('获取省份列表失败: ' + error);
            }
        });
    }

    $(function() {
        $('.summernote').summernote({
            lang: 'zh-CN',
            dialogsInBody: true,
            callbacks: {
                onChange: function(contents, $edittable) {
                    $("input[name='" + this.id + "']").val(contents);
                },
                onImageUpload: function(files) {
                    var obj = this;
                    var data = new FormData();
                    data.append("file", files[0]);
                    $.ajax({
                        type: "post",
                        url: ctx + "common/upload",
                        data: data,
                        cache: false,
                        contentType: false,
                        processData: false,
                        dataType: 'json',
                        success: function(result) {
                            if (result.code == web_status.SUCCESS) {
                                $('#' + obj.id).summernote('insertImage', result.url);
                            } else {
                                $.modal.alertError(result.msg);
                            }
                        },
                        error: function(error) {
                            $.modal.alertWarning("图片上传失败。");
                        }
                    });
                }
            }
        });
    });



</script>


</body>
</html>